package control;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import model.backup.*;

/**
 *
 * @author junjie
 */
//
public abstract class BackupControl {
    public static boolean startBackUp(String backupTime) throws ClassNotFoundException, SQLException, IOException
    {
        String backupFolder = "backup/"+backupTime;
        backupFolder = folderManager.FolderManager.checkAndCreateFolder(backupFolder);
        
        backupAddress(backupFolder);
        backupCustomer(backupFolder);
        backupCustomerAddress(backupFolder);
        backupCustomerTelephone(backupFolder);
        backupGraveYard(backupFolder);
        backupOrderCustomer(backupFolder);
        backupOrder(backupFolder);
        backupOrderLine(backupFolder);
        backupProductFamily(backupFolder);
        backupProduct(backupFolder);
        backupProgramValue(backupFolder);
        backupTombstone(backupFolder);
        backupUser(backupFolder);
        
        return true;
    }
    
    private static void backupAddress(String backupFolder) throws ClassNotFoundException, SQLException, IOException
    {
        BackupAddress[] addresses = model.backup.handlers.BackupAddressHandler.getAddress();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_address.txt", true));
        
        for (int i = 0; i < addresses.length; i++) 
        {
            bw.append(addresses[i].getId()+"\t");
            bw.append(addresses[i].getStreet()+"\t");
            bw.append(addresses[i].getZipCode());
            bw.newLine();
        }
        
        bw.close();
    }
    
    private static void backupCustomer(String backupFolder) throws ClassNotFoundException, SQLException, IOException
    {
        BackupCustomer[] customers = model.backup.handlers.BackupCustomerHandler.getCustomer();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_customer.txt", true));
        
        for (int i = 0; i < customers.length; i++) {
          bw.append(customers[i].getId()+"\t");
          bw.append(customers[i].getName()+"\t");
          bw.append(customers[i].getMortician()+"");
          bw.newLine();
        }
        bw.close();
    }
    
    private static void backupCustomerAddress(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupCustomerAddress[] customerAddresses = model.backup.handlers.BackupCustomerAddressHandler.getCustomerAddress();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_customer_address.txt", true));
        
        for (int i = 0; i < customerAddresses.length; i++) {
            bw.append(customerAddresses[i].getFkCustomer()+"\t");
            bw.append(customerAddresses[i].getFkAddress()+"");
            bw.newLine();  
        }
        bw.close();
    }
    
    private static void backupCustomerTelephone(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupCustomerTelephone[] customerTelephones = model.backup.handlers.BackupCustomerTelephoneHandler.getCustomerTelephone();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_customer_telephone.txt", true));
        
        for (int i = 0; i < customerTelephones.length; i++) {
            bw.append(customerTelephones[i].getNumber()+ "\t");
            bw.append(customerTelephones[i].getFkCustomer()+"");
            bw.newLine();  
        }
        bw.close();
    }
    
    private static void backupGraveYard(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupGraveYard[] graveYards = model.backup.handlers.BackupGraveYardHandler.getBackupGraveYards();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_grave_yard.txt", true));
        
        for (int i = 0; i < graveYards.length; i++) {
            bw.append(graveYards[i].getId()+"\t");
            bw.append(graveYards[i].getName()+"\t");
            bw.append(graveYards[i].getFkAddressId()+"");
            bw.newLine();
        }
        bw.close();
    }
    
    private static void backupOrderCustomer(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupOrderCustomer[] orderCustomers = model.backup.handlers.BackupOrderCustomerHandler.getBackupOrderCustomers();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_order_customer.txt", true));
        
        for (int i = 0; i < orderCustomers.length; i++) {
           bw.append(orderCustomers[i].getFkOrderId()+"\t");
           bw.append(orderCustomers[i].getFkCustomerId()+"\t");
           bw.append(orderCustomers[i].getOrderOrPayer()+"");
           bw.newLine();
        }
        bw.close();
    }
    
    private static void backupOrder(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupOrder[] orders = model.backup.handlers.BackupOrderHandler.getBackupOrders();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_order.txt", true));
        
        for (int i = 0; i < orders.length; i++) {
           bw.append(orders[i].getId()+"\t");
           bw.append(orders[i].getOrderDate()+"\t");
           bw.append(orders[i].getPickupDate()+"\t");
           bw.append(orders[i].getDeliveryDate()+"\t");
           bw.append(orders[i].getInvoiceDate()+"\t");
           bw.append(orders[i].getPaymentReceived()+"\t");
           bw.append(orders[i].getFinished()+"\t");
           bw.append(orders[i].getComment()+"\t");
           bw.append(orders[i].getFkUserId()+"\t");
           bw.append(orders[i].getFkTax()+"\t");
           bw.append(orders[i].getFkTombstoneOrderId()+"");
           bw.newLine();
        }
        bw.close();
    }
    
    private static void backupOrderLine(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupOrderLine[] orderLines = model.backup.handlers.BackupOrderLineHandler.getBackupOrderLines();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_order_line.txt", true));
        
        for (int i = 0; i < orderLines.length; i++) {
            bw.append(orderLines[i].getId()+"\t");
            bw.append(orderLines[i].getAmount()+"\t");
            bw.append(orderLines[i].getUnitPrice()+"\t");
            bw.append(orderLines[i].getFkProductId()+"\t");
            bw.append(orderLines[i].getFkOrderId()+"");
            bw.newLine();  
        }
        bw.close();
    }
    
    private static void backupProductFamily(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupProductFamily[] productFamilys = model.backup.handlers.BackupProductFamilyHandler.getBackupProductFamily();
        BufferedWriter bw = new BufferedWriter (new FileWriter(backupFolder+"/bs_product_family.txt", true));
        
        for (int i = 0; i < productFamilys.length; i++) {
            bw.append(productFamilys[i].getId()+"\t");
            bw.append(productFamilys[i].getName()+"");
            bw.newLine();
        }
        bw.close();
    }
    
    private static void backupProduct(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupProduct[] products = model.backup.handlers.BackupProductHandler.getBackupProduct();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_product.txt", true));
        
        for (int i = 0; i < products.length; i++) {
            bw.append(products[i].getId()+"\t");
            bw.append(products[i].getName()+"\t");
            bw.append(products[i].getPurchasingPrice()+"\t");
            bw.append(products[i].getSalesPrice()+"\t");
            bw.append(products[i].getSzd()+"\t");
            bw.append(products[i].getHasDecoration()+"\t");
            bw.append(products[i].getRemainAfterSale()+"\t");
            bw.append(products[i].getActive()+"\t");
            bw.append(products[i].getSold()+"\t");
            bw.append(products[i].getFkProductFamily()+"");
            bw.newLine();
        }
        bw.close();
    }
    
    private static void backupProgramValue(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupProgramValues[] programValues = model.backup.handlers.BackupProgramValueHandler.getProgramValues();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_program_values.txt", true));
        
        for (int i = 0; i < programValues.length; i++) {
            bw.append(programValues[i].getId()+"\t");
            bw.append(programValues[i].getVat()+"\t");
            bw.append(programValues[i].getEnvironmentTax()+"");
            bw.newLine(); 
        }
        bw.close();
    }
    
    private static void backupTombstone(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupTombstone[] tombstones = model.backup.handlers.BackupTombstoneHandler.getTombstone();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_tombstone_order.txt", true));
        
        for (int i = 0; i < tombstones.length; i++) {
            bw.append(tombstones[i].getId()+"\t");
            bw.append(tombstones[i].getCuffin()+"\t");
            bw.append(tombstones[i].getArea()+"\t");
            bw.append(tombstones[i].getTheRow()+"\t");
            bw.append(tombstones[i].getTheNumber()+"\t");
            bw.append(tombstones[i].getOldName()+"\t");
            bw.append(tombstones[i].getInscription()+"\t");
            bw.append(tombstones[i].getFont()+"\t");
            bw.append(tombstones[i].getRepaint()+"\t");
            bw.append(tombstones[i].getCommission()+"\t");
            bw.append(tombstones[i].getMorticianCommissionNo()+"\t");
            bw.append(tombstones[i].getFkMorticianID()+"\t");
            bw.append(tombstones[i].getFkGraveYardId()+"");
            bw.newLine(); 
        }
        bw.close();
    }
    
    private static void backupUser(String backupFolder) throws ClassNotFoundException, SQLException, IOException{
        BackupUser[] users = model.backup.handlers.BackupUserHandler.getBackupUser();
        BufferedWriter bw = new BufferedWriter(new FileWriter(backupFolder+"/bs_user.txt", true));
        
        for (int i = 0; i < users.length; i++) {
            bw.append(users[i].getId()+"\t");
            bw.append(users[i].getUsername()+"\t");
            bw.append(users[i].getPassword()+"\t");
            bw.append(users[i].getEmail()+"\t");
            bw.append(users[i].getSecuritykey()+"\t");
            bw.append(users[i].getAccessLevel()+"");
            bw.newLine();
        }
        bw.close();
    }
 }